package cn.yunhe.controller;

import cn.yunhe.entity.Admin;
import cn.yunhe.entity.Role;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;


import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
public class AdminAction {
    @RequestMapping("hello")
    public String hello(HttpSession session) {
        String msg = "Hello";
        session.setAttribute("msg", msg);
        return "succ";
    }

    @RequestMapping("login")
    public String login(Admin admin, HttpSession session) {
        //System.out.println("name" + admin.getName());
        session.setAttribute("admin", admin);
        return "redirect:kk";
    }

    @RequestMapping("kk")
    public String kk(HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");
        System.out.println("name:" + admin.getName());
        return "succ";
    }

    @RequestMapping("addRole")
    public String addRole(HttpSession session, Role role) {
        session.setAttribute("role", role);
        return "redirect:showRoleJson";
    }


    @RequestMapping("showRoleJson")
    public ModelAndView showRoleJson(HttpSession session) {
        ModelAndView mv = new ModelAndView();
        Role role = (Role) session.getAttribute("role");
        mv.addObject(role);
        mv.setViewName("showRoleJson");
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("sessAttr.do")
    public String sessAttr(Map<String, Object> map) {
        map.put("adminList", Arrays.asList("李非凡", "白家齐", "败家子"));
        map.put("age", 11);
        return "sessAttr";
    }

    /**
     * 返回json格式数据到页面
     *
     * @param role
     * @return
     */
    @RequestMapping(value = "/role", method = RequestMethod.GET)
    public ModelAndView getRole(@RequestParam(value = "role") Role role) {
        ModelAndView mv = new ModelAndView();
        role.setRoleName("lifeifan");
        role.setNote("kkk");
        mv.setViewName("getRole");
        mv.addObject("role", role);
        return mv;
    }

    /**
     * 使用URL传递参数
     *
     * @param name
     * @return
     */
    @RequestMapping("/pathV/{name}")
    public ModelAndView pathVariable(@PathVariable("name") String name) {
        System.out.println("name===" + name);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("pathV");
        return mv;
    }

    /**
     * 使用RequestBody方式绑定值并以json格式传递对象类型的集合到页面
     *
     * @param admin
     * @return
     */
    @RequestMapping("/findRole")
    public ModelAndView findRole(@RequestBody Admin admin) {
        System.out.println("adminName:" + admin.getName() + ",adminRoleName:" + admin.getRole().getRoleName());
        List<Role> list = new ArrayList<Role>();

        Role role = new Role("猪八戒", "天神");
        Role role1 = new Role("唐僧", "和尚念经");
        Role role2 = new Role("沙和尚", "装洋蒜");
        list.add(role);
        list.add(role1);
        list.add(role2);
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("list", list);
        return mv;
    }

    /**
     * 从页面传递数组到Controller
     *
     * @param admins
     * @return
     */
    @RequestMapping("/delAdmins")
    public ModelAndView delAdmins(@RequestBody List<Admin> admins) {
        ModelAndView mv = new ModelAndView();
        System.out.println(admins.get(0).getId());
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    /**
     * 序列化表单
     *
     * @param admin
     * @return
     */
    @RequestMapping("/adminSerial")
    public ModelAndView adminSerial(Admin admin) {
        System.out.println("adminName====" + admin.getName());
        System.out.println("adminPwd====" + admin.getPassword());
        ModelAndView mv = new ModelAndView();
        mv.setViewName("succ");
        return mv;
    }

    /**
     * Cookie和请求头
     *
     * @param userAgent
     * @param jsessionId
     * @return
     * 输出以下内容:
     * user-agent===Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
     * jsessionId===1rv8ens5boqmr1jkjh6fmvstga
     */
    @RequestMapping("/getHeaderAndCookie")
    public String testHeaderAndCookie(
            @RequestHeader(value = "User-Agent", required = false, defaultValue = "attr") String userAgent,
            @CookieValue(value = "JSESSIONID", required = true, defaultValue = "MyJsessionId") String jsessionId
    ) {
        System.out.println("user-agent===" + userAgent);
        System.out.println("jsessionId===" + jsessionId);
        return null;
    }


}
